package com.zss.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zss.entity.Dept;
import com.zss.entity.WorkOrder;
import com.zss.entity.WorkOrderOA;
import com.zss.mapper.WorkOrderMapper;
import com.zss.service.DeptService;
import com.zss.service.WorkOrderService;
import com.zss.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/workOrder")
public class WorkOrderController {

    @Resource
    private WorkOrderService workOrderService;

    @Resource
    private WorkOrderMapper workOrderMapper;

    @Resource
    private DeptService deptService;

    /**
     * 分页查询
     * @param current 当前页
     * @param rows 每页显示条数
     */
    @PostMapping("")
    public R search(@RequestParam(name = "current", defaultValue = "1") Integer current,
                    @RequestParam(name = "rows", defaultValue = "10") Integer rows) {
        Page<WorkOrder> page = new Page<>(current, rows);
        Page<WorkOrder> workOrderPage = workOrderService.page(page);
        return R.ok(workOrderPage);
    }

    /**
     * 添加
     */
    @PostMapping("save")
    public R<WorkOrder> save(WorkOrder workOrder) {
        R<WorkOrder> validationResult = validateWorkOrder(workOrder);
        if (validationResult != null) {
            return validationResult;
        }
        String orderNo = generateOrderNo();
        QueryWrapper<WorkOrder> qw = new QueryWrapper<>();
        qw.eq("order_no", orderNo);
        List<WorkOrder> list = workOrderService.list(qw);
        //判断工单编号是否重复
        if (!list.isEmpty()) {
            return R.error("工单编号重复！！！");
        }
        workOrder.setOrderNo(orderNo);
        workOrder.setCreateTime(new Date());
        return workOrderService.save(workOrder) ? R.ok("添加成功") : R.error("添加失败");
    }

    /**
     * 根据id修改
     */
    @PostMapping("update")
    public R<WorkOrder> update(WorkOrder workOrder) {
        R<WorkOrder> validationResult = validateWorkOrder(workOrder);
        if (validationResult != null) {
            return validationResult;
        }
        String orderNo = generateOrderNo();
        QueryWrapper<WorkOrder> qw = new QueryWrapper<>();
        qw.eq("order_no", orderNo);
        List<WorkOrder> list = workOrderService.list(qw);
        //判断工单编号是否重复
        if (!list.isEmpty()) {
            return R.error("工单编号重复！！！");
        }
        workOrder.setOrderNo(orderNo);
        return workOrderService.updateById(workOrder) ? R.ok("修改成功") : R.error("修改失败");
    }

    /**
     * 根据id删除
     */
    @PostMapping("delete/{id}")
    public R<WorkOrder> delete(@PathVariable Integer id) {
        return workOrderService.removeById(id) ? R.ok("删除成功") : R.error("删除失败");
    }

    /**
     * 分派接口
     * @param id 工单id
     * @param dept 部门对象
     */
    @PostMapping("fenpai")
    public R<WorkOrder> fenpai(Integer id, Dept dept) {
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.eq("dept_id", dept.getDeptId());
        List<Dept> list = deptService.list(qw);
        //判断部门id是否有效
        if (list.isEmpty()) {
            return R.error("部门id不存在！！！");
        }
        WorkOrder workOrder = workOrderService.getById(id);
        workOrder.setHandleDeptId(dept.getDeptId());
        workOrder.setFenpaiTime(new Date());
        return workOrderService.updateById(workOrder) ? R.ok("修改成功") : R.error("修改失败");
    }

    //查询8月每天的工单总量、超期率
    @PostMapping("selectA")
    public R<List<WorkOrderOA>> selectA() {
        List<WorkOrderOA> workOrderOA = workOrderMapper.selectA();
        return R.ok(workOrderOA);
    }

    /**
     * 查询每个月每个工单类型的工单总量、超期率
     * @param year 查询年份
     * @param month 查询月份
     */
    @PostMapping("selectB")
    public R<List<WorkOrderOA>> selectB(String year, String month) {
        List<WorkOrderOA> workOrderOA = workOrderMapper.selectB(year, month);
        return R.ok(workOrderOA);
    }


    // 验证工单对象
    private R<WorkOrder> validateWorkOrder(WorkOrder workOrder) {
        if (workOrder.getOrderType() == null || workOrder.getOrderType().isEmpty()) {
            return R.error("工单类型不能为空！！！");
        }
        if (workOrder.getTitle() == null || workOrder.getTitle().isEmpty()) {
            return R.error("标题不能为空！！！");
        }
        if (workOrder.getContent() == null || workOrder.getContent().isEmpty()) {
            return R.error("内容不能为空！！！");
        }
        return null; // 验证通过
    }

    // 生成唯一的工单编号
    private String generateOrderNo() {
        StringBuilder sb = new StringBuilder(10);
        for (int i = 0; i < 8; i++) {
            sb.append(new Random().nextInt(10));
        }
        return sb.toString();
    }

}
